#! /usr/bin/perl

use warnings;
use strict;
use XML::Simple;
use Data::Dumper;

print "----------------------------\n";

my $xml = new XML::Simple;
our $CONF = $xml->XMLin("aae.xml", ForceArray => ['stack']);

my $distinct = {};
foreach my $name (keys %{$CONF->{territories}->{territory}}){
	my $values = $CONF->{territories}->{territory}->{$name};
	
	if (exists $distinct->{name}->{$name}){
		print "Duplicate territory name!  $name\n";
	} else {
		$distinct->{name}->{$name} = 1;
	}
	
	foreach my $key (keys %$values){
		$distinct->{$key}->{$CONF->{territories}->{territory}->{$name}->{$key}} = 1;
	}
}

# display all types and owners
print "Owners: " . join(" ", (map {"'$_' "} sort keys %{$distinct->{owner}})) . "\n";
print "Types: " . join(" ", (map {"'$_' "} sort keys %{$distinct->{type}})) . "\n";

# check for duplicate connections
# check for connections to/from places not in territory names
my $map = {};
foreach my $connect (@{$CONF->{connections}->{connect}}){
	print "Connection from '$connect->{from}' to '$connect->{to}' exists\n" and next
		if exists $map->{$connect->{from}}->{connect}->{$connect->{to}} or $map->{$connect->{from}}->{connect}->{$connect->{to}};
	print "From '$connect->{from}' does not exist\n" and next
		unless exists $distinct->{name}->{$connect->{from}};
	print "To '$connect->{to}' does not exist\n" and next
		unless exists $distinct->{name}->{$connect->{to}};
	$map->{$connect->{from}}->{connect}->{$connect->{to}} = 1;
	$map->{$connect->{to}}->{connect}->{$connect->{from}} = 1;
}
print "Map check complete\n";

# display connections by territory
foreach my $name (sort keys %$map){
	# print "$name\n\t" . join(" ", map {"'$_'"} sort keys %{$map->{$name}->{connect}} ) . "\n";
}

# compute starting incomes by ownership
my %total;
foreach my $name (keys %{$CONF->{territories}->{territory}}){
	my $ter = $CONF->{territories}->{territory}->{$name};
	$total{$ter->{owner}} = 0 if $ter->{production} and not exists $total{$ter->{owner}};
	$total{$ter->{owner}} += $ter->{production} if $ter->{production};
}
print "\nStarting income\n";
foreach my $owner (sort keys %total){
	print "$owner: $total{$owner}\n";
}
print "\n";

# check starting piece territory names and are valid by terrain type
my $startConf = $CONF->{start}->{territory};
foreach my $name (sort keys %$startConf){
	print "$name does not exist for starting piece placement\n" unless exists $distinct->{name}->{$name};
}

# show total starting pieces by territory and unit by owner
my (%unitsByOwner, %unitsByTerritory);
foreach my $name (sort keys %$startConf){
	my $units = $startConf->{$name}->{stack};
	
	my $total = 0;
	my $owner = $CONF->{start}->{territory}->{$name}->{owner} || $CONF->{territories}->{territory}->{$name}->{owner};
	foreach my $unit (@$units){
		$total += $unit->{number};
		$unitsByOwner{$owner}->{$unit->{unit}} = 0 unless exists $unitsByOwner{$owner}->{$unit->{unit}};
		$unitsByOwner{$owner}->{$unit->{unit}} += $unit->{number};
	}
	$unitsByTerritory{$name} = $total;
}
#print Dumper %unitsByOwner;
foreach my $name (sort keys %unitsByTerritory){
	print "$name $unitsByTerritory{$name}Å\n";
}

